﻿using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using Ninject;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System;
using System.Globalization;
using System.Windows.Controls;
using System.Net;

namespace InstantWatch {
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window {
		private IProxy proxy;

		ObservableCollection<QueueItem> myItems;

		public MainWindow() {
			InitializeComponent();

			IKernel kernel = new StandardKernel(new NetflixModule());
			proxy = kernel.Get<IProxy>();
			myItems = new ObservableCollection<QueueItem>();
			this.queueList.ItemsSource = myItems;
		}


		private void RequestAuth_Click(object sender, RoutedEventArgs e) {
			proxy.RequestAuthorization();
		}

		private void Go_Click(object sender, RoutedEventArgs e) {
			IEnumerable<QueueItem> items = proxy.GetUserQueue(20);
			
			this.myItems.Clear();
			foreach (var item in items) {
				myItems.Add(item);
			}
		}

		private void QueueItemView_SourceUpdated(object sender, DataTransferEventArgs e) {

		}

		private void Close_Click(object sender, RoutedEventArgs e) {
			Close();
		}

		private void lnkFull_Click(object sender, RoutedEventArgs e) {
			if(WindowStyle == System.Windows.WindowStyle.None) {
				lnkFull.Content = "Full Screen";
				ResizeMode = System.Windows.ResizeMode.CanResize;
				WindowStyle = System.Windows.WindowStyle.SingleBorderWindow;
				WindowState = System.Windows.WindowState.Normal;
				Topmost = false;
			}
			else {
				lnkFull.Content = "Exit Full Screen";
				ResizeMode = System.Windows.ResizeMode.NoResize;
				WindowStyle = System.Windows.WindowStyle.None;
				WindowState = System.Windows.WindowState.Maximized;
				Topmost = true;
			}
		}
	}

	internal class MyCloneConverter : IValueConverter {
		public static MyCloneConverter Instance = new MyCloneConverter();

		public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {

			double a = (double)value;
			double b = double.Parse(parameter.ToString());
			return a + b;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
			throw new NotSupportedException();
		}

	}

	internal class StringUriToTextConverter : IValueConverter {
		public static StringUriToTextConverter Instance = new StringUriToTextConverter();

		public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
			string source = value as string;
			if (source == null) return null;

			try {
				using (WebClient wc = new WebClient()) {
					return wc.DownloadString(source);
				}
			} catch (Exception exp) {
				return source + Environment.NewLine + exp.Message;
			}
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
			throw new NotSupportedException();
		}

	}
}
